//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "uEcriture.h"
 #include "CptaAss.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CDate"
#pragma link "CEdit"
#pragma link "CBitBtn"
#pragma link "CDBGrid"
#pragma link "CEditNum"
#pragma link "Compta"
#pragma link "CTable"
#pragma resource "*.dfm"
#define MODULE_TAG 1210
//---------------------------------------------------------------------------
void __fastcall DetailEcriture(const int IDEcriture, const AnsiString &strLibelle)
{
  TfrmEcr *frmEcr = NULL;
  try
  {
    frmEcr = new TfrmEcr(IDEcriture,strLibelle, NULL);
    frmEcr->ShowModal();
  }
  catch (Exception &e)
  {
    EICDisplayException(TAGL,e);
  }
  delete frmEcr;
}
//---------------------------------------------------------------------------
__fastcall TfrmEcr::TfrmEcr(const int IDEcriture, const AnsiString &strLibelle,TComponent* Owner)
    : CEICForm(Owner)
{
  Currency CurDebit, CurCredit;
  try
  {
    TableHI1->Filter = "IDEcriture=" + IntToStr(IDEcriture) + (strLibelle != EmptyStr ?  " AND LibelleOperation=" + QuotedStr(strLibelle) :  EmptyStr);
    TableHI1->Open();
    edJal->Text = TableHI1CodeJournal->AsString;
    CDate1->Date = TableHI1DateImputation->AsDateTime;
    TableHI1->DisableControls();
    TableHI1->First();
    while (!TableHI1->Eof)
    {
      CurCredit += TableHI1Credit->AsCurrency;
      CurDebit += TableHI1Debit->AsCurrency;
      TableHI1->Next();
    }
    TableHI1->EnableControls();
    CDBGrid1->Total["Debit"] = CurDebit;
    CDBGrid1->Total["Credit"]= CurCredit;
    edEcart->Valeur = CurDebit - CurCredit;
  }
  catch (Exception &e)
  {
    EICDisplayException(TAGL,e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmEcr::CDBGrid1MoveUserCol(TObject *Sender)
{
  int  nLeftDeb   , nLeftCred;
  int  nWidthDeb  , nWidthCred;
  bool toto = true;
  // Repositionne les totaux des colonnes
  CDBGrid1->GetPosColumn( TableHI1Debit , nLeftDeb , nWidthDeb , toto );
  CDBGrid1->GetPosColumn( TableHI1Credit, nLeftCred, nWidthCred, toto);
  // Ecart
  edEcart->Left  = nLeftDeb  + ( nWidthDeb  >> 1);
  edEcart->Width = nLeftCred + ( nWidthCred >> 1) - edEcart->Left;

}
//---------------------------------------------------------------------------


void __fastcall TfrmEcr::FormClose(TObject *Sender, TCloseAction &Action)
{
  try
  {
    TableHI1->Close();
    TableHI1->Filter = EmptyStr;
    TableHI1->Filtered = false;
  }
  catch (Exception &e)
  {
    EICDisplayException(TAGL,e);
  }
}
//---------------------------------------------------------------------------

void __fastcall TfrmEcr::TableHI1CalcFields(TDataSet *DataSet)
{
  if (TableHI1EstCrediteur->AsBoolean)
    TableHI1Credit->AsCurrency = TableHI1Montant->AsCurrency;
  else
    TableHI1Debit->AsCurrency = TableHI1Montant->AsCurrency;
}
//---------------------------------------------------------------------------

void __fastcall TfrmEcr::TableHI1FilterRecord(TDataSet *DataSet, bool &Accept)
{
  Accept = !TableHI1->EstUneLigneAnnulee;
}
//---------------------------------------------------------------------------

